package com.demo.controller.dataManagemant;

import com.demo.common.ReturnCode;
import com.demo.common.model.ObjectPoem;
import com.demo.common.model.ObjectPoet;
import com.demo.service.dataManagement.PoemService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;
import java.util.List;

/**
 * @Classname PoemController
 * @Description TODO
 * @Date 2020/3/11 18:40
 * @Created by yuanhang
 */
public class PoemController extends Controller {
    @Inject
    PoemService poemService;

    public void index() {
        set("router", "/dataManagement/poem");
        render("index.html");
    }

    /**
     * @return
     * @throws
     * @description 条件查询
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void condition(String poemTitle, int page, int limit) {
        Ret ret = new Ret();
        Kv cond = Kv.by("poem_title", poemTitle);
        Page<Record> recordList = poemService.condition(poemTitle, page, limit);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", recordList.getList()).set("count", recordList.getTotalRow());
        renderJson(ret);
    }


    /**
     * @return
     * @throws
     * @description 分页查询
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void paginate(int page, int limit) {
        Ret ret = new Ret();
        Page<Record> recordList = poemService.condition("", page, limit);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", recordList.getList()).set("count", recordList.getTotalRow());
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/11 12:28
     * @Param
     */
    public void detail(int id) {
        List<Record> recordList = Db.template("poem.id", id).find();
        Object obj = recordList.size() > 0 ? set("poem", recordList.get(0)) : set("poem", new ObjectPoet());
        render("detail.html");
    }

    /**
     * @return
     * @throws
     * @description
     * @author yuanhang
     * @date 2020/3/11 12:28
     * @Param
     */
    public void edit(int id) {
        List<Record> recordList = Db.template("poem.id", id).find();
        Object obj = recordList.size() > 0 ? set("poem", recordList.get(0)) : set("poem", new ObjectPoet());
        render("edit.html");
    }

    /**
     * @return
     * @throws
     * @description 新增
     * @author yuanhang
     * @date 2020/3/10 19:02
     * @Param
     */
    public void add(@Para("") ObjectPoem poem) {
        Ret ret = new Ret();
        poem.setCreateTime(new Date());
        poem.setLastUpdateTime(new Date());
        if (poem.save()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description 更新
     * @author yuanhang
     * @date 2020/3/10 19:01
     * @Param
     */
    public void update(@Para("") ObjectPoem poem) {
        Ret ret = new Ret();
        poem.setLastUpdateTime(new Date());
        if (poem.update()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }

    /**
     * @return
     * @throws
     * @description 删除
     * @author yuanhang
     * @date 2020/3/10 19:01
     * @Param
     */
    public void delete(int id) {
        Ret ret = new Ret();
        ObjectPoem poem = new ObjectPoem();
        poem.setId(id);
        if (poem.delete()) {
            ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg());
        } else {
            ret.set("code", ReturnCode.ERROR.getCode()).set("msg", ReturnCode.ERROR.getMsg());
        }
        renderJson(ret);
    }


    /**
     * @return
     * @throws
     * @description 查询诗人
     * @author yuanhang
     * @date 2020/3/16 19:09
     * @Param
     */
    public void poet(String name) {
        Ret ret = new Ret();
        List<Record> recordList = poemService.poet(name);
        ret.set("code", ReturnCode.SUCCESS.getCode()).set("msg", ReturnCode.SUCCESS.getMsg()).set("data", recordList);
        renderJson(ret);
    }
}
